import 'package:eplorex/models/HomeItemModel.dart';
import 'package:flutter/material.dart';

import '../../dao/momentinfo_dao.dart';
import 'moment/momentinfo_page.dart';

class WaterCardWidget extends StatefulWidget {
  const WaterCardWidget({super.key, required this.item, required this.height});

  final HomeItemModel item;
  final double height;

  @override
  State<WaterCardWidget> createState() => _WaterCardWidgetState();
}

class _WaterCardWidgetState extends State<WaterCardWidget> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {
          Navigator.of(context).push(MaterialPageRoute(
              builder: (_) =>
                  MomentInfoDetailsPage(widget.item.momentId?.toInt())));
        },
        child: Container(
          height: widget.height,
          decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                clipBehavior: Clip.antiAlias,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5),
                ),
                child: Image.network(
                  widget.item.imageUrl!,
                  fit: BoxFit.cover,
                  height: widget.height - 80,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 10, right: 10, bottom: 5),
                child: Text(
                  widget.item.title!,
                  style: TextStyle(fontWeight: FontWeight.w600),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 10, right: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        CircleAvatar(
                          radius: 8,
                          backgroundImage: NetworkImage(
                            widget.item.userBaseInfo!.avatar!,
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 5),
                          child: Text(widget.item.userBaseInfo!.userName!,
                              style: TextStyle(fontSize: 12)),
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        buildLike(),
                        Padding(
                          padding: const EdgeInsets.only(left: 5),
                          child: Text(
                            '${widget.item.likeCnt}',
                            style: TextStyle(fontSize: 16),
                          ),
                        )
                      ],
                    )
                  ],
                ),
              )
            ],
          ),
        ));
  }

  Widget buildLike() {
    return GestureDetector(
      onTap: () {
         MomentInfoDao.likeMoment(widget.item.momentId, true);
      },
      child: const Icon(
        Icons.favorite_border,
        size: 16,
      ),
    );
  }
}
